<?php
// @formatter:off
/**
 * @file WCParserBehaviorIF.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-05-19 06:39:24 +0000 (Sun, 19 May 2013) $
 *
 * $Id: WCParserBehaviorIF.php 65 2013-05-19 06:39:24Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/includes/parser/WCParserBehaviorIF.php $
 */
// @formatter:on

//
// Loading father class.
require_once dirname(__FILE__)."/WCParserBehavior.php";

//
// Adding the tag to evaluate by this behavior.
$WCParserBehaviorMatrix["IF"] = "WCParserBehaviorIF";

/**
 * @class WCParserBehaviorIF
 * This is a specific behavior that emulates an IF evaluation.
 * When it's used it looks like:
 * @code
 * <:IF:!:var=name:value=3:/>content<::/>
 * @endcode
 */
class WCParserBehaviorIF extends WCParserBehavior {
	public static $WarningNONAME_VARIABLE = "NONAME_VARIABLE";

	public function check() {
		if($this->_node) {
			if(count($this->_node->params()) < 1) {
				$this->_errorMsg = "IF without parameters";
				$this->_ok = false;
			} else {
				$this->_ok = true;
				$this->_errorMsg = "Ok";
			}
		} else {
			$this->_errorMsg = "IF node is not set";
			$this->_ok = false;
		}

		return $this->isOk();
	}
	public function get() {
		if($this->isOk()) {
			$isTrue = false;
			$params = $this->_node->params();

			$varname = isset($params["var"]) ? $params["var"] : false;
			if(!$varname) {
				$this->_parser->setWarning(WCParserBehaviorIF::$WarningNONAME_VARIABLE, "No variable set", false, false, __CLASS__);
			} else {
				if(!$this->_parser->has($varname)) {
					$this->_parser->setWarning(WCParser::$WarningUNSET_VARIABLE, "Variable '{$varname}' is not set", false, false, __CLASS__);
				} else {
					if(isset($params["value"])) {
						$isTrue = ($this->_parser->$varname == $params["value"]);
					} elseif(isset($params["to"])) {
						$subvarname = $params["to"];
						if(!$this->_parser->has($subvarname)) {
							$this->_parser->setWarning(WCParser::$WarningUNSET_VARIABLE, "Variable '{$subvarname}' is not set", false, false, __CLASS__);
						} else {
							$isTrue = ($this->_parser->$varname == $this->_parser->$subvarname);
						}
					} elseif(isset($params["in"])) {
						$subvarname = $params["in"];
						if(!$this->_parser->has($subvarname)) {
							$this->_parser->setWarning(WCParser::$WarningUNSET_VARIABLE, "Variable '{$subvarname}' is not set", false, false, __CLASS__);
						} elseif(!is_array($this->_parser->$subvarname)) {
							$this->_parser->setWarning(WCParser::$WarningUNSET_VARIABLE, "Variable '{$subvarname}' is not an array", false, false, __CLASS__);
						} else {
							$isTrue = in_array($this->_parser->$varname, $this->_parser->$subvarname);
						}
					} elseif(isset($params["count"])) {
						$isTrue = (is_array($this->_parser->$varname) && count($this->_parser->$varname) == $params["count"]);
					} else {
						if($this->_parser->$varname) {
							$isTrue = true;
						}
					}
				}
				if(isset($params["!"])) {
					$isTrue = !$isTrue;
				}
			}

			if($isTrue) {
				return $this->_node->parseChildren();
			}
		} else {
			return false;
		}
	}
	protected function init() {
		$this->_parser->setWarning(WCParserBehaviorIF::$WarningNONAME_VARIABLE, false, false, "IF invocation but now variable name. Use keyword 'var'");
	}
}
?>